import settings from "../utils/settings";
import { location } from "../utils/getLocation";
import { guiMover } from "../utils/gui";
import { timedisplay , getskulltextures, DrawString, ServerTick } from "../utils/utils";


//Flare timer 3.0
export function flaretimer(){
let flare = {
    Work : false,
    lastflare: "",
    Text : "§a§lFlare",
    flares : {},
    RBGtext(){
        this.Text = "§" + this.flares[this.lastflare].Type[1] + "§lF§a§llare";
        setTimeout(() => {this.Text = "§a§lF§" + this.flares[this.lastflare].Type[1] + "§ll§a§lare";}, 150);
        setTimeout(() => {this.Text = "§a§lFl§" + this.flares[this.lastflare].Type[1] + "§la§a§lre";}, 300);
        setTimeout(() => {this.Text = "§a§lFla§" + this.flares[this.lastflare].Type[1] + "§lr§a§le";}, 450);
        setTimeout(() => {this.Text = "§a§lFlar§" + this.flares[this.lastflare].Type[1] + "§le";}, 600);
        setTimeout(() => {this.Text = "§a§lFlare";}, 750);
    }
}
register("tick", () => {
    if(location.inSkyblock){
        flare.Work = false;
        for (e in flare.flares) {
            flare.flares[e].exist = false;
        };
        //SOS Flare
        World.getAllEntitiesOfType(net.minecraft.entity.item.EntityArmorStand).filter(e => getskulltextures(e.getEntity().func_71124_b(4)) === "ewogICJ0aW1lc3RhbXAiIDogMTY2MjY4Mjc3NjUxNiwKICAicHJvZmlsZUlkIiA6ICI4YjgyM2E1YmU0Njk0YjhiOTE0NmE5MWRhMjk4ZTViNSIsCiAgInByb2ZpbGVOYW1lIiA6ICJTZXBoaXRpcyIsCiAgInNpZ25hdHVyZVJlcXVpcmVkIiA6IHRydWUsCiAgInRleHR1cmVzIiA6IHsKICAgICJTS0lOIiA6IHsKICAgICAgInVybCIgOiAiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS9jMDA2MmNjOThlYmRhNzJhNmE0Yjg5NzgzYWRjZWYyODE1YjQ4M2EwMWQ3M2VhODdiM2RmNzYwNzJhODlkMTNiIiwKICAgICAgIm1ldGFkYXRhIiA6IHsKICAgICAgICAibW9kZWwiIDogInNsaW0iCiAgICAgIH0KICAgIH0KICB9Cn0=").forEach((e)=>{
            if(e.distanceTo(Player.getPlayer()) < 40){
                flare.Work = true;
                flare.lastflare = e.getUUID().toString();
            }
            if(!((e.getUUID().toString()) in flare.flares)){
                flare.flares[e.getUUID().toString()] = {
                    Type: "§5§lSOS",
                    TimeStamp: ServerTick.getTime(),
                    entity: e
                }
            }
            flare.flares[e.getUUID().toString()].exist = true;
        })
        //Alert Flare
        World.getAllEntitiesOfType(net.minecraft.entity.item.EntityArmorStand).filter(e => getskulltextures(e.getEntity().func_71124_b(4)) === "ewogICJ0aW1lc3RhbXAiIDogMTcxOTg1MDQzMTY4MywKICAicHJvZmlsZUlkIiA6ICJmODg2ZDI3YjhjNzU0NjAyODYyYTM1M2NlYmYwZTgwZiIsCiAgInByb2ZpbGVOYW1lIiA6ICJOb2JpbkdaIiwKICAic2lnbmF0dXJlUmVxdWlyZWQiIDogdHJ1ZSwKICAidGV4dHVyZXMiIDogewogICAgIlNLSU4iIDogewogICAgICAidXJsIiA6ICJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlLzlkMmJmOTg2NDcyMGQ4N2ZkMDZiODRlZmE4MGI3OTVjNDhlZDUzOWIxNjUyM2MzYjFmMTk5MGI0MGMwMDNmNmIiLAogICAgICAibWV0YWRhdGEiIDogewogICAgICAgICJtb2RlbCIgOiAic2xpbSIKICAgICAgfQogICAgfQogIH0KfQ==").forEach((e)=>{
            if(!flare.Work && e.distanceTo(Player.getPlayer()) < 40){
                flare.Work = true;
                flare.lastflare = e.getUUID().toString();
            }
            if(!((e.getUUID().toString()) in flare.flares)){
                flare.flares[e.getUUID().toString()] = {
                    Type: "§1§lAlert",
                    TimeStamp: ServerTick.getTime(),
                    entity: e
                }
            }
            flare.flares[e.getUUID().toString()].exist = true;
        })
        //Warning Flare
        World.getAllEntitiesOfType(net.minecraft.entity.item.EntityArmorStand).filter(e => getskulltextures(e.getEntity().func_71124_b(4)) === "ewogICJ0aW1lc3RhbXAiIDogMTY2MjY4Mjg0NTU4NiwKICAicHJvZmlsZUlkIiA6ICIwODFiZTAxZmZlMmU0ODMyODI3MDIwMjBlNmI1M2ExNyIsCiAgInByb2ZpbGVOYW1lIiA6ICJMeXJpY1BsYXRlMjUyNDIiLAogICJzaWduYXR1cmVSZXF1aXJlZCIgOiB0cnVlLAogICJ0ZXh0dXJlcyIgOiB7CiAgICAiU0tJTiIgOiB7CiAgICAgICJ1cmwiIDogImh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMjJlMmJmNmMxZWMzMzAyNDc5MjdiYTYzNDc5ZTU4NzJhYzY2YjA2OTAzYzg2YzgyYjUyZGFjOWYxYzk3MTQ1OCIsCiAgICAgICJtZXRhZGF0YSIgOiB7CiAgICAgICAgIm1vZGVsIiA6ICJzbGltIgogICAgICB9CiAgICB9CiAgfQp9").forEach((e)=>{
            if(!flare.Work && e.distanceTo(Player.getPlayer()) < 40){
                flare.Work = true;
                flare.lastflare = e.getUUID().toString();
            }
            if(!((e.getUUID().toString()) in flare.flares)){
                flare.flares[e.getUUID().toString()] = {
                    Type: "§a§lWarning",
                    TimeStamp: ServerTick.getTime(),
                    entity: e
                }
            }
            flare.flares[e.getUUID().toString()].exist = true;
        })
    }
})

register("step",() => {
    if(flare.Work) flare.RBGtext();
}).setDelay(3)

register("worldLoad", () => {
    flare.lastflare = "";
    for (e in flare.flares) {
        delete flare.flares[e];
    }
})

register("renderOverlay", () => {
    if(!settings.Flaretimer) return;
    if(guiMover.gui.isOpen()){
        DrawString("§5§lSOS §a§lFlare§7§l: §4§l1:14s",guiMover.settings.FlareTimer.x,guiMover.settings.FlareTimer.y,guiMover.settings.FlareTimer.s,true)
    }else{
    if (flare.lastflare && flare.flares[flare.lastflare].exist) {
    if (ServerTick.getTime() - flare.flares[flare.lastflare].TimeStamp < 180000) {
        DrawString(flare.flares[flare.lastflare].Type + " " + (flare.Work ? flare.Text : "§e§lFlare") + "§7§l: §4§l" + timedisplay(180000 - (ServerTick.getTime() - flare.flares[flare.lastflare].TimeStamp)),guiMover.settings.FlareTimer.x,guiMover.settings.FlareTimer.y,guiMover.settings.FlareTimer.s,true)
    }
    }
}
})

register("renderWorld", () => {
    if(!settings.Flaretimer || !settings.ShowOnFlare) return;
    if(!flare.lastflare) return;
    for (uid in flare.flares) {
        if (flare.flares[uid].exist && ServerTick.getTime() - flare.flares[uid].TimeStamp < 180000) {
            Tessellator.drawString(" §l" + timedisplay(180000 - (ServerTick.getTime() - flare.flares[uid].TimeStamp)) , flare.flares[uid].entity.getRenderX() , flare.flares[uid].entity.getRenderY()  + 1 , flare.flares[uid].entity.getRenderZ()
            ,(settings.flarecolor.getRGB()),false,0.05,false);
        }
    }
})

}